package com.gw.smscheck.controller.sys;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import com.gw.smscheck.entity.sys.*;
import com.gw.smscheck.service.sys.DepartmentService;
import com.gw.smscheck.util.ResultConversion;
import com.gw.smscheck.util.ResultObj;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 部门管理
 *
 * @author zhouxiaoying
 * @ClassName: DepartmentController
 * @date 2017年10月9日 上午8:47:59
 * @
 */
@Controller
@RequestMapping("/departmentController")
@Api(description="【部门管理接口】")
public class DepartmentController {

    @Autowired
    private DepartmentService departmentService;


    /**
     * 部门管理界面
     *
     * @param
     * @return String
     * @throws
     * @Title: goDept
     */
    @RequestMapping(value = "/goDept")
    @ApiIgnore
    // @ResponseBody
    public String goDept() {
        return "sys/department";
    }

    /**
     * 根据id查询
     * @param department
     * @return
     */
    @ApiOperation(value="根据id查询", notes="根据id查询")
    @RequestMapping(value="/findById",method={RequestMethod.POST})
    @ResponseBody
    public String findById(Department department){
        return ResultObj.successResultObj(departmentService.findById(department));
    }

    /**
     * 分页查询
     * @param department
     * @return
     */
    @ApiOperation(value="分页查询", notes="分页查询")
    @RequestMapping(value="/pageSelect",method={RequestMethod.POST,RequestMethod.GET})
    @ResponseBody
    public String pageSelect(Department department){
//        PageInfo<Department> pageInfo = departmentService.pageSelect(department);
//        return ResultConversion.ToEasyUIDataGrid(pageInfo).toJSONString();
        return ResultObj.successResultObj(departmentService.pageSelect(department));
    }

    /**
     * 部门树形列表
     *
     * @param department 部门
     * @return QueryResult<DeptTreeGridModel>
     * @throws Exception 抛异常
     * @Title: deptIndex
     */
    @ApiOperation(value="部门树形列表", notes="部门树形列表")
    @RequestMapping(value="/deptIndex",method={RequestMethod.POST})
    @ResponseBody
    public QueryResult<DeptTreeGridModel> deptIndex(Department department) throws Exception {
        return departmentService.findTreeDeptInfos(department);
    }

    /**
     * 部门树结构下拉框
     *
     * @param department 部门
     * @return List<TreeModel>
     * @throws Exception 抛异常
     * @Title: queryAllMenus
     */
    @ApiOperation(value="部门树结构下拉框", notes="部门树结构下拉框")
    @RequestMapping(value="/queryAllMenus",method={RequestMethod.POST})
    @ResponseBody
    public List<TreeModel> queryAllMenus(Department department) throws Exception {
        return departmentService.findAllMenus(department);
    }

    /**
     * 添加菜单
     *
     * @param department 部门
     * @return ParameterObject
     * @throws Exception 抛异常
     * @Title: saveDept
     */
    @ApiOperation(value="添加菜单", notes="添加菜单")
    @RequestMapping(value="/saveDept",method={RequestMethod.POST})
    @ResponseBody
    public ResultObj saveDept(HttpServletRequest request, Department department) {
        ResultObj result = new ResultObj();
        Integer i = 0;
        if (department.getId() != null && department.getId() != 0) {
            i = departmentService.updateDepartment(department);
        } else {
            Integer userId = (Integer) request.getSession().getAttribute("userId");
            department.setCreateUserId(userId);
            i = departmentService.saveDepartment(department);
        }
        if (i > 0) {
            result.setMsg("操作成功");
            result.setCode(ResultObj.CODE_SUCCESS);
        } else {
            result.setMsg("操作失败");
            result.setCode(ResultObj.CODE_FAIL);
        }
        return result;
    }

    /**
     * 修改菜单
     *
     * @param department 部门
     * @return ParameterObject
     * @throws Exception 抛异常
     * @Title: saveDept
     */
    @ApiOperation(value="修改菜单", notes="修改菜单")
    @RequestMapping(value="/updateDept",method={RequestMethod.POST})
    @ResponseBody
    public ResultObj updateDept(Department department) {
        ResultObj result = new ResultObj();
        Integer i = 0;
        if (department.getId() != null && department.getId() != 0) {
            i = departmentService.updateDepartment(department);
        } else {
            i = departmentService.saveDepartment(department);
        }
        if (i > 0) {
            result.setMsg("操作成功");
            result.setCode(ResultObj.CODE_SUCCESS);
        } else {
            result.setMsg("操作失败");
            result.setCode(ResultObj.CODE_FAIL);
        }
        return result;
    }

    /**
     * 根据id删除部门
     *
     * @param response 响应
     * @param id     id
     * @return ParameterObject
     * @throws Exception 抛异常
     * @Title: deleteDdpt
     */
    @ApiOperation(value="删除部门", notes="删除部门")
    @RequestMapping(value="/deleteDdpt",method={RequestMethod.POST})
    @ResponseBody
    public ResultObj deleteDdpt(HttpServletResponse response,Department department) {
        ResultObj result = new ResultObj();
        Integer i = 0;
        i = departmentService.deleteDepartment(department.getId());
        if (i > 0) {
            result.setMsg("操作成功");
            result.setCode(ResultObj.CODE_SUCCESS);
        } else {
            result.setMsg("操作失败");
            result.setCode(ResultObj.CODE_FAIL);
        }
        return result;
    }

    /**
     * 所有部门查询
     *
     * @return List<Department> 返回类型 所有部门集合
     * @Title: queryDepartAll 
     * @throws 
     */
    @ApiOperation(value="所有部门查询", notes="所有部门查询")
    @RequestMapping(value="/queryDepartAll",method={RequestMethod.GET})
    @ResponseBody
    public String queryDepartAll() {
        List<Department> departAll = departmentService.queryDepartAll();
        return JSON.toJSONString(departAll);
    }

    /**
     * treeText
     * @Title: getByDescribes 
     * @throws 
     */
    @RequestMapping(value="department/getByDescribes",method={RequestMethod.GET})
    @ResponseBody
    @ApiIgnore
    public String getByDescribes(Department department) {
        return departmentService.getByDescribes(department);
    }

}
